//
//  NSDate+ZLIOSKit.m
//
//  Created by Jiaozl on 2021/4/19.
//  
//

#import "NSDate+ZLIOSKit.h"
#import "NSObject+ZLIOSKit.h"

/// 默认格式
static NSString *const zl_default_format = @"yyyy-MM-dd HH:mm:ss";

@implementation NSDate (ZLIOSKit)

+ (NSDateFormatter *)_zlIOSkit_formatter {
    static NSDateFormatter *dateFormatter = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        dateFormatter = [[NSDateFormatter alloc] init];
    });
    return dateFormatter;
}

+ (NSString *)zlIOSkit_currentDateStrWithFormat:(NSString *)formatter {
    return [self zlIOSkit_dateStrFromDate:NSDate.date andFormat:formatter];
}

+ (NSString *)zlIOSkit_dateStrWithStr:(NSString *)senddate sendFormat:(NSString *)sendFormat retureFormat:(NSString *)format {
    NSDateFormatter *dateFormatter = [self _zlIOSkit_formatter];
    [dateFormatter setDateFormat:[sendFormat zlIOSkit_isValidStr] ? sendFormat : zl_default_format];
    NSDate *date = [dateFormatter dateFromString:senddate];
    NSString *retureStr = [NSDate zlIOSkit_dateStrFromDate:date andFormat:format];
    return retureStr;
}

+ (NSString *)zlIOSkit_dateStrFromDate:(NSDate *)date andFormat:(NSString *)format {
    NSDateFormatter *dateformatter = [self _zlIOSkit_formatter];
    [dateformatter setDateFormat:[format zlIOSkit_isValidStr] ? format : zl_default_format];
    NSString *locationString = [dateformatter stringFromDate:date];
    return locationString;
}

+ (NSDate *)zlIOSkit_dateFromDateStr:(NSString *)dateStr andFormat:(NSString *)format {
    NSDateFormatter *dateFormatter = [self _zlIOSkit_formatter];
    [dateFormatter setDateFormat:[format zlIOSkit_isValidStr] ? format : zl_default_format];
    NSDate *date = [dateFormatter dateFromString:dateStr];
    return date;
}

+ (NSString *)zlIOSkit_dateStrFromTimeInterval:(NSTimeInterval)timeInterval andFormat:(NSString *)format {
    
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:timeInterval];
    NSDateFormatter *formatter = [self _zlIOSkit_formatter];
    [formatter setDateFormat:[format zlIOSkit_isValidStr] ? format : zl_default_format];
    NSString *dateString = [formatter stringFromDate:date];
    return dateString;
}

@end
